<?php
/**
*+-------------提供给IMBATV做内容编辑的接口--------+
*--------------------------------------------------+
*+-----------获取某一个比赛------------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Api\Controller;

class ImbaController extends PublicController 
{

	

	/**
	*赛事列表
	*根据页码进行获取，按照时间倒序
	*@author susy.liang<linagling@ilongyuan.com.cn>
	*/
	public function matches(){

		$data = array();
		$mevtObj = A('Api/Match', 'Event');

		$total = $mevtObj->total();
		if($total){

			$matchSrv = D('Common/Match', 'Service');

			$page = I('page', 0, 'intval');
			$number = I('number', 20, 'intval');			
			$res = $matchSrv->where(array('status'=>array('egt', $matchSrv::STATUS_PEDDING)))->field('id,title');
			if($page)
				$res = $res->page($page, $number);
			$res = $res->select();

			foreach ($res as $key => $value) {
				unset($value['_id']);
				array_push($data, $value);
			}

		}

		//$this->success('操作成功', array('list'=>$data, 'total'=>$total));
		$this->success('操作成功', array('list'=>$data));

	}

	/**
	*获取比赛列表，并包含比赛的视频列表
	*根据页码p进行获取
	*/
	public function videos(){

		$videoSrv = D('Common/Video', 'Service');

		$matchId = I('match_id', 0, 'intval');
		if(!$matchId)
			$this->error(self::$API_ERROR['PARAM'], '参数错误');

		$page = I('page', 0, 'intval');
		$number = I('number', 20, 'intval');
		$map = array(
				'status'=>$videoSrv::STATUS_PASS,
				'type'=>$videoSrv::TYPE_MATCH,
				'match_id'=>$matchId
			);
		$ids = $videoSrv->where($map)->field('id,title,group_id,match_hour,match_time');
		if($page)
			$ids = $ids->page($page, $number);
		$ids = $ids->select();

		if(empty($ids))
			$this->error(self::$API_ERROR['MATCH'], '暂无比赛数据');

		$data = array();
		$matchEvent = A('Api/Match', 'Event');
		$rgService = D('Common/RaceGroup', 'Service');
		$videoField = array('title', 'uri', 'pre_uri', 'thumb', 'time', 'time_point');
		$teamField = array('name', 'logo');
		foreach ($ids as $key => $value) {
			$videos = $matchEvent->video(intval($value['id']));

			if($videos['videos']){
				$video = array();
				foreach ($videos['videos'] as $vk => $vd) {
					$video[] = $this->field_filter($vd, $videoField);
				}
			}

			if($videos['team']){
				$team = array();
				foreach ($videos['team'] as $tk => $td) {
					$team[] = $this->field_filter($td, $teamField);
				}
			}
			$data[] = array(
						'id'=>$value['id'],
						'match_hour' => $value['match_hour'],
						'match_date' => date('Y-m-d',$value['match_time']),
						'title'=>$value['title'],
						'race_group'=>$videos['group_title'],
						'teams'=>$team?$team:array(),
						'videos'=>$video?$video:array()
				);
		}

		$this->success('', $data);
	}

	/**
	*总的赛事
	*@author susy<liangling@ilongyuan.com.cn>
	*/
	public function totalMatch(){
		$total = A('Api/Match', 'Event')->total();
		$this->success('操作成功', array('total'=>$total));
	}

	/**
	*赛事下所有比赛总数
	*@author susy<liangling@ilongyuan.com.cn>
	*/
	public function totalRace(){
		$videoSrv = D('Common/Video', 'Service');

		$matchId = I('match_id', 0, 'intval');
		if(!$matchId)
			$this->error(self::$API_ERROR['PARAM'], '参数错误');

		$map = array(
				'status'=>$videoSrv::STATUS_PASS,
				'type'=>$videoSrv::TYPE_MATCH,
				'match_id'=>$matchId
			);
		$total = $videoSrv->where($map)->count();
		
		$this->success('操作成功', array('total'=>$total));
		
	}

	/**
	*获取单局比赛下的视频列表信息
	*获取条件是比赛标识
	*@author susy<liang.liang@ilongyuan.com.cn>
	*/
	public function video(){

		$eventObj = A('Api/Match', 'Event');

		$id = I('id', 0, 'intval');
		if(!$id)
			$this->error(self::$API_ERROR['PARAM'], '参数错误');
		
		$infos = $eventObj->video($id, $uid);
		if(!$infos)
			$this->error(self::$API_ERROR['MATCH'], '比赛信息有误');

		$match = $eventObj->info(intval($infos['match_id']));
		$infos['match_title'] = $match['title'];

		$raceGroup = D('Common/RaceGroup', 'Service')->field('title')->find(intval($infos['group_id']));
		$infos['race_group'] = $raceGroup['title'];

		$fieldMap = array('id', 'thumb', 'title', 'score', 'total_score', 'round','detail', 
						'videos','team', 'action', 'schedule', 'match_title', 'race_group'
						);
		$infos = $this->field_filter($infos, $fieldMap);
		
		$this->success('请求成功', $infos);
	}
}